<template>
  <el-card shadow="never">
    <el-button
      icon="el-icon-plus"
      size="small"
      type="primary"
      @click="handleEdit(false, {})"
      class="fr mb20"
    />
    <e-table
      ref="eTable"
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :total="total"
    >
      <el-table-column slot="action" fixed="right" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                <a>编辑</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleEdit(true, scope.row, true)">
                <a>查看</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">
                <a class="red">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
    <contract-modal ref="contractModal" @refresh="handleGetList" />
  </el-card>
</template>

<script>
import ContractModal from './ContractModal'
import { getContract, deleteContract } from '@/api/supplier'
import { getSession } from '@/utils/util'

export default {
  components: {
    ContractModal
  },
  data() {
    return {
      mdl: this.$route.params.obj || getSession('record'),
      tableData: [],
      columns: [
        { label: '合同编号', prop: 'contractNo' },
        {
          label: '合同名称',
          prop: 'contractName'
        },
        {
          label: '合同类型',
          prop: 'contractType'
        },
        {
          label: '供应商名称',
          prop: 'supplierName'
        },
        {
          label: '合同签订日期',
          prop: 'signDate'
        },
        {
          label: '合同结束日期',
          prop: 'expiryDate'
        },
        {
          label: '合同金额',
          prop: 'contractSum'
        },
        {
          label: '付款条款',
          prop: 'paymentTerms'
        },
        {
          label: '第三方',
          prop: 'thirdParty'
        },
        { slot: 'action' }
      ],
      loading: false,
      total: 1
    }
  },
  async mounted() {
    console.log('contract mdl', this.mdl)
    this.handleGetList()
  },
  methods: {
    async handleGetList() {
      this.loading = true
      const res = await getContract({ supplierCode: this.mdl.supplierCode })
      console.log('contract res', res)

      if (res && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    },
    handleEdit(bool, row, view = false) {
      this.$nextTick(() => {
        this.$refs.contractModal.edit(bool, row, view)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteContract(row.contractId)
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    }
  }
}
</script>